#include"SqList.h"
void SLInit(SL* ps) {
	ps->arr = NULL;
	ps->capacity = 0;
	ps->size = 0;
}
void SLDestroy(SL* ps) {
	if (ps->arr) {
		free(ps->arr);
	}
	ps->capacity = 0;
	ps->size = 0;
}
void SLCheckCapacity(SL* ps) {
	assert(ps);
	if (ps->size == ps->capacity) {
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, sizeof(SLDataType) * newCapacity);
		if (tmp == NULL) {
			perror("realloc fail!!");
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
}
void SLPushBack(SL* ps, SLDataType x) {
	assert(ps);
	SLCheckCapacity(ps);
	ps->arr[ps->size++] = x;
}
void SLPrint(SL s) {
	for (int i = 0; i < s.size; i++) {
		printf("%d ", s.arr[i]);
	}
	if (s.size == 0) {
		printf("NULL");
	}
	printf("\n");
}
void SLPushFront(SL* ps, SLDataType x) {
	assert(ps);
	SLCheckCapacity(ps);
	for (int i = ps->size; i > 0; i--) {
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}
void SLPopBack(SL* ps) {
	assert(ps);
	assert(ps->size);
	ps->size--;
}
void SLPopFront(SL* ps) {
	assert(ps);
	assert(ps->size);
	for (int i = 1; i < ps->size; i++) {
		ps->arr[i-1] = ps->arr[i];
	}
	ps->size--;
}
void SLInsert(SL* ps, int pos, SLDataType x) {
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);
	for (int i = ps->size - 1; i >=pos ; i--) {
		ps->arr[i + 1] = ps->arr[i];
	}
	ps->arr[pos] = x;
	ps->size++;
}
void SLErase(SL* ps, int pos) {
	assert(ps);
	assert(ps->size);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos; i < ps->size-1; i++) {
		ps->arr[i] = ps->arr[i+1];
	}
	ps->size--;
}
int SLFind(SL s, SLDataType x) {
	for (int i = 0; i < s.size; i++) {
		if (s.arr[i] == x) {
			return i;
		}
	}
	return -1;
}